/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ro.unibuc.hobbygroups.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import ro.unibuc.hobbygroups.domain.Picture;
import ro.unibuc.hobbygroups.service.PicturesService;

public class PicturesController extends MultiActionController {

	private PicturesService picturesService;
	private transient Logger log = Logger.getLogger(PicturesController.class);
	
	/**
	 * upload
	 */
	public ModelAndView upload(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest.getFile("file");

		Picture picture = new Picture();
		picture.setInitialName(multipartFile.getOriginalFilename());
		picture.setDescription(ServletRequestUtils.getStringParameter(request,"notes"));
		picture.setType(multipartFile.getContentType());

		this.picturesService.create(picture, multipartFile.getBytes());

		return new ModelAndView("redirect:pictures.htm");
	}

	public ModelAndView listAll(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView model = new ModelAndView("pictures");
		model.addObject("pictures", picturesService.findAll());
		return model;
	}

	/**
	 * download
	 */
	public ModelAndView download(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		long id = ServletRequestUtils.getRequiredLongParameter(request, "id");
		String thumbParam = ServletRequestUtils.getStringParameter(request, "thumb"); 
		boolean thumb = StringUtils.hasText(thumbParam) ? Boolean.parseBoolean(thumbParam) : false;
		
		if (log.isDebugEnabled()) {
			log.debug("Request pic:" + request.getRequestURL().toString());
		}
		
		Picture picture = this.picturesService.findPicture(id);

		response.setContentType(picture.getType());
		
		byte[] content = null;
		
		if (thumb) {
			content = picturesService.getThumbContent(picture);
		} else {
			content = picturesService.getPictureContent(picture);
		}
		
		response.setContentLength(content.length);
		
		// inline => the file is shown in browser not popup for download !!!!
		response.setHeader("Content-Disposition",
				"inline; filename=\"" + picture.getInitialName() + "\"");
		// response.setHeader("Content-Disposition","attachment; filename=\"" +
		// file.getInitialName() +"\"");
		
		FileCopyUtils.copy(content, response.getOutputStream());
		return null;
	}

	//
	// /**
	// * delete
	// */
	// public ModelAndView delete(HttpServletRequest request,
	// HttpServletResponse response) throws Exception {
	// int id = ServletRequestUtils.getRequiredIntParameter(request, "id");
	//
	// this.picturesService.delete(id);
	//
	// return new ModelAndView("redirect:files.htm");
	// }
	
	@Required
	public void setPicturesService(PicturesService picturesService) {
		this.picturesService = picturesService;
	}

}
